package com.atguigu.gulimall.order.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.atguigu.gulimall.order.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues声明需要监听的所有队列
     * 1、Message message 原生消息详细信息：消息头 + 消息体
     * 2、T<发送的消息类型>  OrderReturnReasonEntity content
     * 3、Channel channel  当前传输数据的通道
     *
     * Queue:可以有很多客户端来监听，只要收到消息，队列就会删除消息，只能有一个客户端收到消息
     *
     * 1、订单服务启动多个;同一个消息，只能是一个客户端收到消息
     * 2、只有一个消息处理完才能接收消息
     *
     * @RabbitListener  可以标在类和方法上（监听哪些队列即可）
     * @RabbitHandler   可以标在方法上(重载不同消息)
     *
     * （发送端，消费端）消息确认
     */
//    @RabbitListener(queues = {"hello-java-queue"})
//    public void receiveMessage(Message message,
//                               OrderReturnReasonEntity content,
//                               Channel channel){
//        //byte[] body = message.getBody();//消息体
//        //MessageProperties messageProperties = message.getMessageProperties();//消息头
//        //1.
//        //System.out.println("接收到消息。。。内容：" + message.getBody() + "==>类型：" + message.getClass());
//        //2.
//        //System.out.println("接收到消息。。。内容：" + content);
//        //3.
//        System.out.println("接收到消息。。。内容：" + content);
//    }

    @RabbitHandler
    public void receiveMessage(Message message,
                               OrderReturnReasonEntity content,
                               Channel channel){
        System.out.println("接收到消息。。。内容：" + content);
        //通道内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //签收货物
        try {
            if(deliveryTag % 2 == 0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物。。。。。。" + deliveryTag);
            }else{
                //退货：requeue=false  丢弃,requeue=true 发回服务器，服务器重新入队列
                //long deliveryTag, boolean muliple, boolean requeue
                channel.basicNack(deliveryTag,false,true);
                //long deliveryTag, boolean requeue
                //channel.basicReject();
                System.out.println("拒收了货物。。。。。。" + deliveryTag);
            }



        } catch (IOException e) {
            //网络中断或者MQ挂了
            e.printStackTrace();
        }
    }


    @RabbitHandler
    public void receiveMessage2(OrderEntity content){
        //System.out.println("接收到消息。。。内容：" + content);
    }

}